//
//  AddQRCodeController.swift
//  Order
//
//  Created by Lazyloading on 2020/7/14.
//  Copyright © 2020 lazyloading. All rights reserved.
//

import UIKit
import ZLPhotoBrowser
import  PKHUD

/// 添加二维码
class AddQRCodeController: UIViewController {

    @IBOutlet weak var bottomConstraints: NSLayoutConstraint!
    @IBOutlet weak var topConstraints: NSLayoutConstraint!
    @IBOutlet weak var typeField: UITextField!
    @IBOutlet weak var accountField: UITextField!
    @IBOutlet weak var codeImg: UIImageView!
    var codeUlr : String?
    
    @IBOutlet weak var finishBtn: UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.loadDefaultSetting();
        self.initSubViews();
    }
    
    // MARK: 👉🏻 🐷Life cycle🐷
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews();
        self.topConstraints.constant =  25;
        self.bottomConstraints.constant = SafeBottom_H + 25;
        
    }
    // MARK: 👉🏻 🐷System Delegate🐷
    // MARK: 👉🏻 🐷Custom Delegate🐷
    // MARK: 👉🏻 🐷Event  Response🐷
    @IBAction func finishAction(_ sender: Any) {
        ZMLog("完成");
        if codeUlr?.count == 0 || codeUlr == nil {
            HUD.flash(.label("请上传二维码"), delay: 1);
            return;
        }
        let param = [
            "account" : self.accountField.text ?? "",
            "methodName" : self.typeField.text ?? "",
            "qrCodeUrl" : self.codeUlr ?? ""
        ];
        
        MoyaRequest(.addCode(param as Dictionary<String, Any>), success: { (response : BaseResponse<[String : Any]>) in
            if response.respCode == 200{
                HUD.flash(.label("添加成功"), delay: 1);
                DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
                    self.navigationController?.popViewController(animated: true);
                }
            }else{
                HUD.flash(.label("添加失败，请稍后再试"), delay: 1);
            }
        }) { (error) in
            HUD.flash(.label("添加失败，请稍后再试"), delay: 1);
        }
    }
    
    

    
    @objc func addCodeImg(tap : UITapGestureRecognizer){
        
        weak var weakSelf = self;
        let actioinSheet = ZLPhotoActionSheet.init();
        actioinSheet.configuration.maxSelectCount = 1;
        actioinSheet.configuration.maxPreviewCount = 10;
        actioinSheet.sender = self;
        actioinSheet.selectImageBlock = {images,assets,isOriginal in
            weakSelf?.codeImg.image = images?.first;
            weakSelf?.uploadImage(image: (images?.first)!);
        };
        actioinSheet.showPreview(animate: true);

        ZMLog("选择照片");
    }
    
    @objc func textfieldValueChanged(textfield : UITextField) {
        ZMLog(textfield.text);
        
        if self.accountField.text!.count  > 0 && self.typeField.text!.count > 0 {
            self.finishBtn.isUserInteractionEnabled = true;
            self.finishBtn.backgroundColor = ZMColor.Orange.orange1;
        }else{
            self.finishBtn.isUserInteractionEnabled = false;
            self.finishBtn.backgroundColor = ZMHexColor("e4e4e4")
        }
        
    }
    // MARK: 👉🏻 🐷Private Methods🐷
    func uploadImage(image : UIImage) {
        weak var weakSelf = self;

        HUD.show(.label("上传中......"))
        MoyaRequest(.uploadData([image]), success: { (response  :  BaseResponse<[String : Any]>) in
            HUD.hide();
            if response.respCode == 200{
                weakSelf!.codeUlr = (response.data!["fileName"] as! String);
            }else{
                HUD.flash(.label("上传失败"),delay: 1);
            }
        }) { (error) in
            HUD.hide();
            HUD.flash(.label("上传失败"),delay: 1);
        }
    }
    // MARK: 👉🏻 🐷Lazy loading🐷
    // MARK: 👉🏻 🐷Init SubViews🐷
    func loadDefaultSetting()  {
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(addCodeImg(tap:)));
        self.codeImg.addGestureRecognizer(tap);
        self.codeImg.isUserInteractionEnabled = true;
        self.navigationItem.title = "添加二维码";
        self.codeImg.layer.cornerRadius = 3;
        self.codeImg.layer.borderWidth = 1;
        self.codeImg.layer.borderColor = ZMColor.Black.black4.cgColor;
        self.finishBtn.layer.cornerRadius = 10;
        self.finishBtn.layer.masksToBounds = true;
        self.finishBtn.backgroundColor = ZMHexColor("e4e4e4");
        
        self.accountField.addTarget(self, action: #selector(textfieldValueChanged), for: .editingChanged);
        self.typeField.addTarget(self, action: #selector(textfieldValueChanged), for: .editingChanged);
        
    }
    func initSubViews()  {
        
    }


}
